package com.qian.apiserver.controller;

import com.qian.apiserver.entity.GmUser;
import com.qian.apiserver.entity.GmWithdrawal;
import com.qian.apiserver.model.Order;
import com.qian.apiserver.model.User;
import com.qian.apiserver.model.Withdrawal;
import com.qian.apiserver.model.WithdrawalModel;
import com.qian.apiserver.service.IUserService;
import com.qian.apiserver.service.IWithdrawalService;
import com.sun.xml.internal.rngom.parse.host.Base;
import com.yaowk.api.common.BaseException;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/game/withdrawal")
@Api(tags = "提现记录")
public class WithdrawalController{
    @Autowired
    private IWithdrawalService withdrawalService;
    @Autowired
    private IUserService userService;
    @PostMapping("/save")
    @ApiOperation("修改状态")
    public void save(Byte status){
        Withdrawal withdrawal = new Withdrawal();
        withdrawal.setStatus(status);
        withdrawalService.save(withdrawal);
    }
    @GetMapping("/all")
    @ApiOperation("提现订单(不填所有订单 时间查询 用户编号查询userCode 支付方式查询)")
    public Map find(PageReq pageReq, WithdrawalModel model){
           if (!StringUtils.isEmpty(model.getUserName())) {
               Pattern pattern = Pattern.compile("[0-9]*");
               if (pattern.matcher(model.getUserName()).matches()) {
                   User user = new User();
                   user.setUserCode(Long.parseLong(model.getUserName()));
                   user = userService.findOne(user);
                   model.setUserId(user.getId());
                   return withdrawalService.find(pageReq, model);
               } else {
                   User user = new User();
                   List list = new ArrayList();
                   user.setUsername(model.getUserName());
                   PageResp<User> pageResp = userService.find(pageReq, user);
                   for (User user1 : pageResp.getList()) {
                       model.setUserId(user1.getId());
                       list.addAll(withdrawalService.findName(pageReq, model));
                   }
                   Map map = new LinkedHashMap();
                   map.put("totalPage", pageResp.getTotalPage());
                   map.put("totalRow", pageResp.getTotalRow());
                   map.put("pageSize", pageResp.getPageSize());
                   map.put("pageNumber", pageResp.getPageNumber());
                   map.put("list", list);
                   return map;
               }
           }
        return withdrawalService.find(pageReq,model);
    }

    @GetMapping("/withdrawalAmount")
    @ApiOperation("充值总金额")
    private BigDecimal withdrawalAmount(){
        List<Withdrawal> withdrawals = withdrawalService.findAll();
        Double money = 0.0;
        for(Withdrawal withdrawal:withdrawals){
            money += withdrawal.getMoney().doubleValue();
        }
        return new BigDecimal(money);
    }

}
